package com.ai_daily.aitiantian.base;

import android.app.Activity;
import android.app.Application;
import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.tencent.bugly.crashreport.CrashReport;

/**
 *
 **/
public class BaseApp extends Application {

    private static BaseApp instance;

    private Activity mCurrentActivity;

    public static BaseApp getInstance() {
        return instance;
    }

    static { //不主动加载加固so, 只提供静态入口
    }

    @Override
    protected void attachBaseContext(Context base) {
        super.attachBaseContext(base);
        instance = this;

    }

    @Override
    public void onCreate() {
        super.onCreate();
        registerLifeActivityCB();
        CrashReport.initCrashReport(getApplicationContext(), "b5afe32deb", false);
    }

    @Override
    public void onTerminate() {
        super.onTerminate();
    }

    private void registerLifeActivityCB() {
        registerActivityLifecycleCallbacks(new ActivityLifecycleCallbacks() {
            @Override
            public void onActivityCreated(@NonNull Activity activity, @Nullable Bundle savedInstanceState) {
                mCurrentActivity = activity;
            }

            @Override
            public void onActivityStarted(@NonNull Activity activity) {
                mCurrentActivity = activity;
            }

            @Override
            public void onActivityResumed(@NonNull Activity activity) {
                mCurrentActivity = activity;
            }

            @Override
            public void onActivityPaused(@NonNull Activity activity) {
//                String className = activity.getComponentName().getClassName();
                mCurrentActivity = activity;
            }

            @Override
            public void onActivityStopped(@NonNull Activity activity) {
                mCurrentActivity = activity;
            }

            @Override
            public void onActivitySaveInstanceState(@NonNull Activity activity, @NonNull Bundle outState) {
                mCurrentActivity = activity;
            }

            @Override
            public void onActivityDestroyed(@NonNull Activity activity) {
                mCurrentActivity = activity;
            }
        });
    }


    public Activity getCurrentActivity(){
        return mCurrentActivity;
    }

    private boolean isNetworkAvailable() {
        ConnectivityManager connectivityManager = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo activeNetworkInfo = connectivityManager.getActiveNetworkInfo();
        return activeNetworkInfo != null && activeNetworkInfo.isConnected();
    }
}
